package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> getHomeProductList() {
        String sql = "select * from product order by id desc limit 2";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        ProductModel productModel = null;
        try {
            while (resultSet.next()) {
                productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setName(resultSet.getString("name"));
//                productModel.setImgHref(resultSet.getString("imgUrl"));
//                productModel.setPrice(resultSet.getString("price"));
//                productModel.setContent(resultSet.getString("diy_stuff"));
                list.add(productModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return list;
    }

    @Override
    public List<ProductModel> getFindAll() {
        String sql = "select * from product order by id desc limit 8";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        ProductModel productModel = null;
        try {
            while (resultSet.next()) {
                productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setName(resultSet.getString("name"));
                productModel.setImgHref(resultSet.getString("imgUrl"));
                productModel.setPrice(resultSet.getDouble("price"));
                productModel.setContent(resultSet.getString("diy_stuff"));
                list.add(productModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return list;
    }

    @Override
    public int add(ProductModel productModel) {
        int a = GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.ProductDao.add", productModel);
        GetSqlSession.commit();
        return a;
    }

    /**
     * 通过菜系id去查询当前菜系的所有产品
     * @param id
     * @return
     */
    @Override
    public List<ProductModel> findById(ProductModel productModel) {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.ProductDao.findById",productModel);
    }

    @Override
    public int count(int cookstyleid) {

        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.ProductDao.count",cookstyleid);
    }
}
